// 图类型转换
#include "./model/ad-list.h"
#include "./model/graph.h"

/// @brief 邻接矩阵转邻接表
/// @param graph 原邻接矩阵
/// @return 转换后的邻接表
AdList toAdList(Graph graph)
{
	// 初始化结果
	AdList list = createAdList();
	int i, j;
	// 插入顶点
	for (i = 0; i < graph->vertexNum; i++)
	{
		adListInsertVex(list, graph->vertexs[i]);
	}
	// 插入边
	for (i = 0; i < graph->vertexNum; i++)
	{
		for (j = 0; j < graph->vertexNum; j++)
		{
			// 先跳过自己
			if (i != j && graph->edges[i][j] != INFINITE)
			{
				adListInsertArc(list, i, j, graph->edges[i][j]);
			}
		}
	}
	return list;
}

/// @brief 邻接表转邻接矩阵
/// @param adList 传入邻接表的指针
/// @return 转换后的邻接矩阵
Graph toMGraph(AdList adList)
{
	// 初始化结果
	Graph graph = createGraph();
	int i;
	// 写入顶点
	for (i = 0; i < adList->vertexNum; i++)
	{
		insertVex(graph, adList->vertexs[i].data);
	}
	// 写入边
	EdgeNode *pointer;
	for (i = 0; i < adList->vertexNum; i++)
	{
		pointer = adList->vertexs[i].firstEdge;
		while (pointer != NULL)
		{
			insertArc(graph, i, pointer->adjvex, pointer->weight);
			pointer = pointer->next;
		}
	}
	return graph;
}

int main()
{
	Graph graph = createGraph();
	for (char c = '1'; c <= '7'; c++)
	{
		insertVex(graph, c);
	}
	int start[] = {0, 0, 1, 4, 1, 3, 4, 2, 5, 0};
	int end[] = {1, 4, 3, 3, 2, 5, 6, 5, 6, 3};
	int weight[] = {3, 5, 7, 10, 2, 9, 1, 11, 3, 20};
	insertArcs(graph, start, end, weight, sizeof(start) / sizeof(int), 1);
	printGraph(graph);
	printf("\n");
	AdList list = toAdList(graph);
	printAdList(list);
	printf("\n");
	Graph g = toMGraph(list);
	printGraph(g);
	return 0;
}